package com.jumi.microservice.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jumi.microservice.common.utils.EnumUtil;
import com.jumi.microservice.dto.excel.ExportExcelCommonResponse;
import com.jumi.microservice.dto.export.ExportCapital;
import com.jumi.microservice.dto.export.ExportCapitalRequest;
import com.jumi.microservice.dto.reponse.JmUserReponse;
import com.jumi.microservice.entity.UserWallet;
import com.jumi.microservice.enumerate.WalletTypeEnum;
import com.jumi.microservice.mapper.UserWalletMapper;
import com.jumi.microservice.service.IMemberService;
import com.jumi.microservice.service.IUserExternalService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangjingdao
 */
@Service
public class ExportWalletService {
    @Resource
    UserWalletMapper userWalletMapper;
    @DubboReference
    IMemberService iMemberService;
    @DubboReference
    private IUserExternalService iUserExternalService;
    @Resource
    AdminCapitalDetailsService adminCapitalDetailsService;
    @Resource
    WalletAccountService walletAccountService;

    public ExportExcelCommonResponse<ExportCapital> exportExcelCommonResponse(ExportCapitalRequest request) {
        ExportExcelCommonResponse<ExportCapital> exportCapitalResponseExportExcelCommonResponse = new ExportExcelCommonResponse<>();
        exportCapitalResponseExportExcelCommonResponse.setFileName("资金明细导出" + System.currentTimeMillis() + "xslx");
        List<ExportCapital> exportCapitalRespons = new ArrayList<>();
        QueryWrapper<UserWallet> userWalletQueryWrapper = new QueryWrapper<>();
        userWalletQueryWrapper.orderByDesc("create_time");
        if (request.getIds() != null) {
            userWalletQueryWrapper.in("id", request.getIds());
        }
        if (request.getEndTime() != null && request.getStartTime() != null) {
            userWalletQueryWrapper.between("create_time", request.getStartTime(), request.getEndTime());
        }
        if (request.getPhone() != null) {
            Integer uid = iUserExternalService.getUidByMobile(request.getPhone());
            if (uid != null && uid > 0) {
                userWalletQueryWrapper.eq("user_id", uid);
            }else{
//                userWalletQueryWrapper.eq("user_id", DelFlag.NORMAL);
                return exportCapitalResponseExportExcelCommonResponse;
            }
        }
        //会员等级
        if (request.getMembershipLevel() != null) {
//            JmUserQueryRequest jmUserQueryRequest = new JmUserQueryRequest();
//            jmUserQueryRequest.setLevel(request.getMembershipLevel());
//            ResponseResult<Page> pageR = iMemberService.getMemberByRequest(jmUserQueryRequest, 1, -1);
//            List<JmUserReponse> jmUserResponses = pageR.getData().getRecords();
//            List<Integer> ids = jmUserResponses.stream().map(JmUserReponse::getId).collect(Collectors.toList());
//            if (!ids.isEmpty()) {
//                userWalletQueryWrapper.in("user_id", ids);
//            }else{
//                return exportCapitalResponseExportExcelCommonResponse;
//            }
            userWalletQueryWrapper.eq("user_level",request.getMembershipLevel());
        }
        if (request.getType() != null) {
            userWalletQueryWrapper.eq("type", request.getType());
        }
        List<UserWallet> userWalletList = userWalletMapper.selectList(userWalletQueryWrapper);
        Map<Long, JmUserReponse> userMap = adminCapitalDetailsService.getUserInfoMap(userWalletList.stream().map(w -> w.getUserId().intValue()).collect(Collectors.toList()));
        Map<Integer,Object> levelMap = walletAccountService.memberLevel();
        for (UserWallet userWallet : userWalletList) {
            ExportCapital exportCapital = new ExportCapital();
            exportCapital.setUid(userWallet.getUserId());
            exportCapital.setName("");
            exportCapital.setPhone("");
            if(userMap.containsKey(userWallet.getUserId())){
                exportCapital.setName(userMap.get(userWallet.getUserId()).getTruename());
                exportCapital.setPhone(userMap.get(userWallet.getUserId()).getMobile());
                if(levelMap.containsKey(userMap.get(userWallet.getUserId()).getLevel().getValue())){
                    exportCapital.setLevel(levelMap.get(userMap.get(userWallet.getUserId()).getLevel().getValue())
                            .toString());
                }
            }
            if(userWallet.getAmount().compareTo(BigDecimal.ZERO)<0){
                exportCapital.setOutAmount(userWallet.getAmount().negate());
            }else{
                exportCapital.setInAmount(userWallet.getAmount());
            }
            exportCapital.setType(EnumUtil.getByCode(userWallet.getType(), WalletTypeEnum.class).getMsg());
            exportCapital.setRemark(userWallet.getRemark());
            exportCapitalRespons.add(exportCapital);
        }
        exportCapitalResponseExportExcelCommonResponse.setData(exportCapitalRespons);
        return exportCapitalResponseExportExcelCommonResponse;
    }

}
